/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.simpleLibrary;

import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 *
 * @author vramik
 */
public class LibraryManagerImpl implements LibraryManager {

    private Map<Reader, List<Book>> loans = new HashMap<Reader, List<Book>>();
    
    @Deprecated
    public List<Book> getLoanBooksFromReader(Reader reader){
        return Collections.unmodifiableList(loans.get(reader));
    }
    
    @Deprecated
    public Reader getReaderWhoHasSpecifiedBook(Book book) {
        Set<Reader> readers = loans.keySet();
        Reader wantedReader = null;
        
        for (Reader reader : readers) {
            for(Book loanBook : loans.get(reader)) {
                if (loanBook.equals(book)){
                    wantedReader = reader;
                }
            }
        }
        return wantedReader;
    }
    
    @Override
    public void loanBooks(Set<Book> books, Reader reader) {
        List<Book> currentBooks = loans.get(reader);
        for (Book book : books) {
            book.setLoan(true);
            currentBooks.add(book);
        }
        loans.put(reader, currentBooks);
    }

    @Override
    public void giveBooksBack(Set<Book> books, Reader reader) {
        List<Book> currentBooks = loans.get(reader);
        for (Book book : books) {
            book.setLoan(false);
            currentBooks.remove(book);
        }
        loans.put(reader, currentBooks);
    }

    @Override
    public Set<Book> findBooksBy(BookAttributeType bookAttributeType, Object value) {
        
        Set<Book> books = new HashSet<Book>();
        BookManager bm = new BookManagerImpl();
        
        for (Book book : bm.findAllBooks()) {
            switch (bookAttributeType) {
                case AUTHOR:
                    if (book.getAuthor().equals((String) value)) {
                        books.add(book);
                    }
                    break;
                case ISBN:
                    if (book.getIsbn().equals((String) value)) {
                        books.add(book);
                    }
                    break;
                case TITLE:
                    if (book.getTitle().equals((String) value)) {
                        books.add(book);
                    }
                    break;
                case YEAR:
                    if (book.getYear().equals((Integer) value)) {
                        books.add(book);
                    }
                    break;
                case LOAN:
                    if (Boolean.valueOf(book.isLoan()).equals((Boolean) value)) {
                        books.add(book);
                    }
                    break;
                default:
                    break;
            }
        }
        return books;
    }
    
}
